package com.spring.student.Dao;
import com.spring.student.systemDB.Score;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;


@Component("scoreManage")
public class ScoreManageImpl implements ScoreManage{
    @Autowired
   private JdbcTemplate jdbcTemplate;

    @Override
    public void addScore(Score score) {
        String sql = "insert into scores(number,Chinese,math,English) values(?,?,?,?)";
        Object[] obj = new Object[]{
                score.getNumber(),
                score.getChinese(),
                score.getMath(),
                score.getEnglish()
        };
        int num = jdbcTemplate.update(sql, obj);
        if (num <= 0) {
            System.out.println("插入学生成绩记录失败");

        }
    }
    @Override
    public void updateScore(String number, String course, Double score) {
        String sql = "update scores set "+course+"=? where number=?";
        Object[] params = new Object[]{score, number};
        int num = jdbcTemplate.update(sql, params);
        if (num <= 0) {
            System.out.println("更新学生成绩记录失败");
        }
    }

    @Override
    public void queryScoreByNum(String number) {

        String sql = "select * from scores where number=?";
        RowMapper<Score> rowMapper;
        rowMapper = new BeanPropertyRowMapper<Score>(Score.class);
        Score score = jdbcTemplate.queryForObject(sql, rowMapper, number);
        System.out.println(score);
    }

    @Override
    public void queryAllScore() {
        String sql = "select * from scores";
        RowMapper<Score> rowMapper;
        rowMapper = new BeanPropertyRowMapper<Score>(Score.class);
        List<Score>scores = jdbcTemplate.query(sql, rowMapper);
        for (Score score : scores) {
            System.out.println(score);
        }
    }
}
